<?php
/**
 * Created by PhpStorm.
 * User: wangjun
 * Date: 2016-10-26
 * Time: 13:55
 */

namespace Home\Model;
use Think\Model;

class ScheduleModel extends Model{
    public function push_dpa($requester,$dpasum){
        $personnel = D('personnel');
        $user = D('user');
        $tel_info = $personnel->field('tel')->where("requester='{$requester}'")->select();
        $tel_admin = $personnel->field('tel')->where("requester='admin'")->select();
        $telinfos = array_column($tel_info,'tel');
        $teladmins = array_column($tel_admin,'tel');
        $hdtime = time()-3600*48;
        $arr['tel'] = array('in',$telinfos);
        $where['tel'] = array('in',$teladmins);
        $arr['hdtime'] = $where['hdtime'] = array('egt',$hdtime);
        $infoopenids = $user->field('openid')->where($arr)->select();
        $adminopenids = $user->field('openid')->where($where)->select();
        $access_token = access_token();
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$access_token}";
        if($infoopenids){
            foreach($infoopenids as $k=>$v){
                // $content = '以下单位的委托已完成  ('.$dpasum.')请查阅 !';
                $content = '您的委托('.$dpasum.')已完成，如须了解更多信息，请进入自助查询！';
                //使用url编码
                $content = urlencode($content);
                $content_arr = array('content' => "{$content}");
                $reply_arr = array('touser' => "{$v['openid']}", 'msgtype' => 'text', 'text' => $content_arr);
                //编码成json格式
                $post = json_encode($reply_arr);
                //url解码
                $post = urldecode($post);
                //dump($post);exit;
                $info = https_request($url,$post);
            }
        }
       if($adminopenids){
           foreach($adminopenids as $k=>$v){
               $content = '以下单位的委托已完成  '.$requester.'('.$dpasum.')请查阅 !';
               //使用url编码
               $content = urlencode($content);
               $content_arr = array('content' => "{$content}");
               $reply_arr = array('touser' => "{$v['openid']}", 'msgtype' => 'text', 'text' => $content_arr);
               //编码成json格式
               $post = json_encode($reply_arr);
               //url解码
               $post = urldecode($post);
               //dump($post);exit;
               $info = https_request($url,$post);
           }
       }
    }

    public function seach(){
        $dpasum = I('get.dpasum');
        if($dpasum){
            $where['dpasum'] = array('like',"%".$dpasum."%");
        }
        $coun = $this->where($where)->count();
        $page = new \Think\Page($coun,20);
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $list = $this->where($where)->limit($page->firstRow,$page->listRows)->order('wtsj desc')->select();
        return array(
            'list' => $list,
            'page_str' => $page->show(),
        );
    }


}